package com.uzai.console.service.alipaymch.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.console.common.utils.DesensitizeUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.constant.Const;
import com.uzai.console.dto.alipaymchbatch.AlipayMchBatchDetailQuery;
import com.uzai.console.dto.alipaymchbatch.AlipayMchBatchQuery;
import com.uzai.console.entity.AlipayMchBatch;
import com.uzai.console.entity.AlipayMchBatchDetail;
import com.uzai.console.entity.UserTakeBal;
import com.uzai.console.enums.AlipayMchBatchDetailStatusEnum;
import com.uzai.console.enums.AlipayMchBatchStatusEnum;
import com.uzai.console.mapper.AlipayMchBatchDetailMapper;
import com.uzai.console.mapper.AlipayMchBatchMapper;
import com.uzai.console.mapper.UserTakeBalMapper;
import com.uzai.console.service.alipaymch.AlipayMchBatchDetailService;
import com.uzai.console.service.alipaymch.AlipayMchBatchService;
import com.uzai.console.vo.alipaymchbatch.AlipayMchBatchDetailVo;
import com.uzai.console.vo.alipaymchbatch.AlipayMchBatchVo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Batcher: lucher
 * @Date: 2021/12/14 16:46
 * @Description:
 */
@Service
public class AlipayMchBatchDetailServiceImpl implements AlipayMchBatchDetailService {

    private static Logger logger = LoggerFactory.getLogger(AlipayMchBatchDetailServiceImpl.class);

    @Autowired
    private AlipayMchBatchDetailMapper alipayMchBatchDetailMapper;
    @Autowired
    private UserTakeBalMapper userTakeBalMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询支付宝个人批量转账记录明细列表
     * @batchor liuqi
     * @date 2021/8/13 11:38
     * @param alipayMchBatchDetailQuery
     * @return null
     */
    public Page<AlipayMchBatchDetailVo> findAlipayMchBatchDetailList(AlipayMchBatchDetailQuery alipayMchBatchDetailQuery){
        //新建返回到页面的LIST对象
        List<AlipayMchBatchDetailVo> alipayMchBatchDetailVoList = new ArrayList<>();

        //执行分页查询对象
        Page<AlipayMchBatchDetail> page = new Page<>();
        alipayMchBatchDetailQuery.convert(page);
        ArrayList<AlipayMchBatchDetail> alipayMchBatchDetailList =  alipayMchBatchDetailMapper.findByList(page, alipayMchBatchDetailQuery);
        if(alipayMchBatchDetailList != null && alipayMchBatchDetailList.size() > 0){
            for (AlipayMchBatchDetail alipayMchBatchDetail : alipayMchBatchDetailList){
                AlipayMchBatchDetailVo alipayMchBatchDetailVo = new AlipayMchBatchDetailVo();
                BeanUtils.copyProperties(alipayMchBatchDetail, alipayMchBatchDetailVo);
                //状态
                if(StringUtils.isNotBlank(alipayMchBatchDetail.getStatus())){
                    AlipayMchBatchDetailStatusEnum alipayMchBatchDetailStatusEnum = AlipayMchBatchDetailStatusEnum.getByKey(alipayMchBatchDetail.getStatus());
                    if(alipayMchBatchDetailStatusEnum != null){
                        alipayMchBatchDetailVo.setStatusName(alipayMchBatchDetailStatusEnum.getDesc());
                    }
                }

                //判断是否需要脱敏
                Boolean tuominFlag = true;
                String cmstype_wxcontacts_wxid_verified =  (String)redisTemplate.opsForValue().get(String.format(Const.SmsType.CMSTYPE_WXCONTACTS_WXID_VERIFIED, alipayMchBatchDetailQuery.getMerId()));
                if(StringUtils.isNotBlank(cmstype_wxcontacts_wxid_verified)){
                    tuominFlag = false;
                }

                //是否脱敏处理
                if(tuominFlag){
                    alipayMchBatchDetailVo.setPayeeAccount(DesensitizeUtil.around(Tools.getStr(alipayMchBatchDetailVo.getPayeeAccount()),3,2));
                    alipayMchBatchDetailVo.setPayeeName(DesensitizeUtil.left(Tools.getStr(alipayMchBatchDetailVo.getPayeeName()),1));
                }

                //提现id
                if(StringUtils.isNotBlank(alipayMchBatchDetail.getOutBizNo())){
                    UserTakeBal userTakeBal = userTakeBalMapper.selectById(Long.valueOf(alipayMchBatchDetail.getOutBizNo()), alipayMchBatchDetail.getMerId());
                    if(userTakeBal != null){
                        alipayMchBatchDetailVo.setUserId(userTakeBal.getUserId());
                        alipayMchBatchDetailVo.setTradeno(userTakeBal.getTradeno());
                    }
                }
                alipayMchBatchDetailVoList.add(alipayMchBatchDetailVo);
            }
        }

        //返回到页面分页对象
        Page<AlipayMchBatchDetailVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(alipayMchBatchDetailVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

}
